package yxy.game.pm2.module.tip;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.game.pm2.message.NotificationMessage;
import yxy.game.pm2.module.bandit.BanditHandle;
import yxy.game.pm2.module.coin.CoinModule;
import yxy.game.pm2.module.conquest.ConquestModule;
import yxy.game.pm2.module.dungeon.DungeonModule;
import yxy.game.pm2.module.fortdefeat.FortDefeatModule;
import yxy.game.pm2.module.friend.FriendModule;
import yxy.game.pm2.module.gacha.GachaHeroModule;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.mail.MailBean;
import yxy.game.pm2.module.shop.handle.ShopHandle;
import yxy.game.pm2.module.tip.checker.*;
import yxy.game.pm2.module.tower.TowerModule;
import yxy.game.pm2.module.union.player.UnionPlayer;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class TipHandle {

    public void updateTip(String playerCode, TipType type, boolean open) {
        boolean isOpen = TipModule.get(playerCode, type);

        if (open == isOpen) {
            return;
        }
        TipModule.set(playerCode, type, open);

        NotificationMessage.TipNotification.Builder notify = NotificationMessage.TipNotification.newBuilder();
        notify.setTip(packTip(type, open));
        MessageEmitterHandler.getInstance().send(playerCode, notify.build());
    }

    public NotificationMessage.Tip packTip(TipType type, boolean open) {
        NotificationMessage.Tip.Builder tip = NotificationMessage.Tip.newBuilder();
        tip.setId(type.getNumber());
        tip.setOpen(open);
        return tip.build();
    }

    public void all(String playerCode) {
        Map<String, Boolean> allTip = TipModule.all(playerCode);
        Set<Entry<String, Boolean>> entrySet = allTip.entrySet();
        for (Entry<String, Boolean> entry : entrySet) {
            String code = entry.getKey();
            boolean open = entry.getValue();
            TipType type = TipType.forCode(code);
            if (type == null) {
                continue;
            }
            NotificationMessage.TipNotification.Builder notify = NotificationMessage.TipNotification.newBuilder();
            notify.setTip(packTip(type,open));
            MessageEmitterHandler.getInstance().send(playerCode, notify.build());
        }
    }

    /**
     * 整体更新
     */
    public void update(String playerCode) {
        ConquestModule.get(playerCode).check();// 征战
        new BanditHandle().updateBanditTip(playerCode);// 剿匪刷新
        TowerModule.get(playerCode);// 修炼塔
        GachaHeroModule.get(playerCode).check();// 招募
        FriendModule.get(playerCode).check();// 好友
        MailBean.checkSystemMail(playerCode);// 检查未领取的系统邮件
        MailBean.check(playerCode); // 邮件
        CoinModule.get(playerCode).checkTimes();// 钱币兑换
        DungeonModule.checkTimes(playerCode);// 每日副本
        FortDefeatModule.get(playerCode).check();// 过关斩将
        UnionPlayer unionPlayer = UnionPlayer.get(playerCode);// 联盟
        if (unionPlayer != null) {
            unionPlayer.check();
        }
        new HeroHandle().updateHeroTip(playerCode);// 武将
        new Checker_排行榜().check(playerCode);// 排行榜膜拜
        ShopHandle.checkTip(playerCode);//商店

        //推送
        all(playerCode);

    }

    static public void init() {
        TipSubscriber_工坊.getInstance();
        TipSubscriber_成就.getInstance();
        TipSubscriber_日常任务.getInstance();
        TipSubscriber_神器任务.getInstance();
        TipSubscriber_神器升级.getInstance();
        TipSubscriber_神器技能.getInstance();
        TipSubscriber_神器精炼.getInstance();
        TipSubscriber_神器铸魂.getInstance();
        TipSubscriber_联盟科技.getInstance();
        TipSubscriber_碎片兑换.getInstance();
        TipSubscriber_武将.getInstance();
    }

}
